<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :visible="visible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
<!--        <a-col :md="8">-->
          <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="开通时间">
              <a-select v-model="yearDate">
                <a-select-option
                  v-for="item in years"
                 :label="item.label"
                 :value="item.value">
                  {{item.value}}年
                </a-select-option>
              </a-select>
          </a-form-item>
<!--        </a-col>-->
            <!--          <a-col :md="8">-->
<!--            <a-form-item-->
<!--              :labelCol="labelCol"-->
<!--              :wrapperCol="wrapperCol"-->
<!--              label="开通时间">-->
<!--              <date-select v-model="yearDate"/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务器类别">
            <a-input placeholder="请输入服务器类别" v-decorator="[ 'dataBaseCategory', validatorRules.category]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="数据库名称">
          <j-dict-select-tag :triggerChange="true" placeholder="请选择服务器名称" ref="databaseName" dictCode="dataBaseName" v-decorator="['dataBaseValue', validatorRules.dataBaseValue]"/>
        </a-form-item>
        <a-row :gutter="20">
            <a-form-item label="服务参数"  :labelCol="labelCol"
                         :wrapperCol="wrapperCol">
            </a-form-item>
          </a-row>
        <a-row :gutter="20">
          <a-col :md="8">
            <a-form-item
              :labelCol="labelRow"
              :wrapperCol="wrapperRow"
              label="版本">
              <j-dict-select-tag :triggerChange="true" placeholder="请选择数据库版本" ref="dataBaseEdition" dictCode="dataBaseEdition" v-decorator="['dataBaseEdition', validatorRules.dataBaseEdition]"/>

            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="10">
              <a-form-item
                :labelCol="labelRowCheckbox"
                :wrapperCol="wrapperRowCheckbox"
                label="维护内容">
                <j-multi-select-tag
                  dictCode="serviceProjects"
                  type="checkbox"
                  ref="serviceProjects"
                  v-model="serviceProjects"
                  :disabled="dis">
                </j-multi-select-tag>
              </a-form-item>
<!--            <a-form-item-->
<!--              :labelCol="labelRow"-->
<!--              :wrapperCol="wrapperRow"-->
<!--              label="维护内容">-->
<!--              <a-select v-decorator="['serviceProjects', {}]" placeholder="请选择">-->
<!--                <a-select-option value="">请选择</a-select-option>-->
<!--                <a-select-option value="1">64G</a-select-option>-->
<!--                <a-select-option value="2">128G</a-select-option>-->
<!--              </a-select>-->
<!--&lt;!&ndash;              <j-dict-select-tag :triggerChange="true" placeholder="请选择内存" dictCode="sys_technology_type" v-decorator="[ 'sysTechnologyType', validatorRules.sysTechnologyType]"/>&ndash;&gt;-->
<!--            </a-form-item>-->
          </a-col>
        </a-row>
        <a-row :gutter="24">
<!--          <a-col :md="8">-->
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="计费要素">
              <a-select v-decorator="['dataBaseElements', {}]" placeholder="请选择计费要素">
                <a-select-option value="">请选择计费要素</a-select-option>
                <a-select-option value="1">套</a-select-option>
              </a-select>
            </a-form-item>
<!--          </a-col>-->
        </a-row>
        <a-row :gutter="24">
<!--          <a-col :md="8">-->
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="资费">
              <a-input placeholder="资费"  v-decorator="[ 'expense', validatorRules.expense]" />(元/年)

            </a-form-item>
<!--          </a-col>-->
        </a-row>
      </a-form>
    </a-spin>
    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">返回</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="confirmLoading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  import pick from 'lodash.pick'
  import {saveDataBaseList,editDataBaseList } from '@/api/api'
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDate from '@/components/jeecg/JDate'
  export default {
    components: {
      // VueDatepickerLocal,
      JMultiSelectTag,
      JSelectDepart,
      JDate,
    },
    name: "cloudDataBaseModel",
    data(){
      return{
        yearDate:'',
        serviceProjects:"",
        ser:{},
        years:[],
        form: this.$form.createForm(this),
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 14 },
          sm: { span: 2 },
        },
        wrapperCol: {
          xs: { span: 16 },
          sm: { span: 8 },
        },
        labelRow: {
          xs: { span: 14 },
          sm: { span: 10 },
        },
        wrapperRow: {
          xs: { span: 14 },
          sm: { span: 14 },
        },
        labelRowCheckbox: {
          xs: { span: 10 },
          sm: { span: 8 },
        },
        wrapperRowCheckbox: {
          xs: { span: 10 },
          sm: { span: 14 },
        },
        dis:false,
        hiding:true,
        drawerWidth:1000,
        confirmLoading: false,
        disableSubmit:false,
        validatorRules:{
        },
      }
    },
    mounted() {
      this.init();
    },
    methods:{
      init(){
        this.getYear();
      },
      getYear(){//获取年
        let date = new Date();
        let current_year = date.getFullYear();
        // this.queryParam.yearDate=current_year;
        this.years=[];
        for (let i = 0; i < 30; i++) {
          let y = current_year - i;
          this.years.push({value:(y),label:(y)+ "年"});
          // this.yearDate.push(y);
          if(y=='1970'){
            return
          }
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      add () {
        this.edit({});
      },
      edit(record){
        if(record.yearDate==null){
          this.yearDate='';
        } else {
         this.yearDate = record.yearDate+'年' ;
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'dataBaseCategory','dataBaseValue', 'dataBaseName','dataBaseEdition','serviceProjects','dataBaseElements','expense','yearDate' ))
        });
      },
      handleCancel () {
        this.close()
      },
      handleSubmit () {
        debugger
        let self = this ;
        let dataBaseEdi =self.$refs.dataBaseEdition.value;
        let servicePro =self.$refs.serviceProjects.value;
        let dataValue=self.$refs.databaseName.value;
        let name="";
        if(dataValue !="" || dataValue!=undefined){
          name=self.$refs.databaseName.dictOptions[dataValue].text
        }
        let dataBaseEdition="";
        let param="";
        let year = this.yearDate.toString();
        if(year.indexOf("年") != -1 ){
          year= year.substring(0,4)
        }
        if(dataBaseEdi!=""){
          dataBaseEdition =self.$refs.dataBaseEdition.dictOptions[dataBaseEdi].text
          param=dataBaseEdition;
        }
        if(servicePro!=""){
          let aa=[];
          if(servicePro !="" || servicePro!=undefined){
            if(servicePro.indexOf(",") != -1 ){
              aa= servicePro.split(",");
              for (let i = 0; i <aa.length ; i++) {
                if(i>0){
                  param=param+","+self.$refs.serviceProjects.dictOptions[aa[i]].text;
                }else {
                  param=param+" "+self.$refs.serviceProjects.dictOptions[aa[i]].text;
                }

              }
            }else {
              param=param+" "+self.$refs.serviceProjects.dictOptions[servicePro].text;
            }
          }
        }
        let params={params:param};
        let yearDate={yearDate:year};
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            values.dataBaseName=name;
            // values.dataBaseValue=dataValue
            that.confirmLoading = true;

            let formData = Object.assign(this.model, values,params,yearDate);
            let obj;

            if(!this.model.id){
              obj=saveDataBaseList(formData);
            }else{
              obj=editDataBaseList(formData);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;

              that.close();
            })

          }
        })
      },
    },
  }
</script>

<style scoped>

</style>